package com.hmall.gateway.filter;


import lombok.extern.apachecommons.CommonsLog;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

//@Component // 是一个组件
public class MyGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取请求对象
        System.out.println("请求对象:{}" + exchange.getRequest());
        //执行的逻辑
        System.out.println("执行了 MyGlobalFilter 过滤器 pre"); // 执行前

        //放行
        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            System.out.println("执行了 MyGlobalFilter 过滤器 post"); // 执行后
        }));
    }

    @Override
    public int getOrder() {
        // 优先级，数字越小优先级越高
        return 0;
    }
}
